package com.aliengen.thingple.driverapp.ui.activities;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.aliengen.thingple.driverapp.R;
import com.aliengen.thingple.driverapp.data.models.Anomaly;
import com.aliengen.thingple.driverapp.data.models.Report;
import com.aliengen.thingple.driverapp.ui.fragments.AnomaliesListFragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.FragmentById;
import org.androidannotations.annotations.ViewById;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
@EActivity(R.layout.activity_anomaly_list)
public class AnomalyListActivity extends AbstractBaseActivity  implements AnomaliesListFragment.OnListFragmentListener {

    @Extra("report")
    Report report;

    @ViewById
    protected TextView truckTitle;

    @ViewById
    protected TextView customerTitle;

    @ViewById
    protected TextView customerAddressTitle;

    @ViewById
    protected TextView cylindersCount;

    @FragmentById
    AnomaliesListFragment anomalies_list;

    @AfterViews
    public void setNames() {
        setTextView(truckTitle, R.string.truck_plate_number, report.truck.getPlateNumber());
        setTextView(customerTitle, R.string.customer_name, report.customer.name);
        setTextView(customerAddressTitle, R.string.customer_address, report.customer.address);
        setTextView(cylindersCount, R.string.cylinders_count, Integer.toString(report.cylinders.size()));

        anomalies_list.getAdapter().updateList(report.anomalies);
    }

    @Override
    public void onResume() {
        super.onResume();
        anomalies_list.getAdapter().updateList(report.anomalies);
    }

    @Override
    public void onClickInteraction(Anomaly item) {

    }

    @Override
    public void onLongClickInteraction(Anomaly item) {

    }

    public void onClickAdd(View view){
        Intent nextScreen = new Intent(getApplicationContext(), CylinderReadActivity_.class);
        if(anomalies_list != null) {
            report.anomalies = anomalies_list.getAdapter().getList();
        }
        nextScreen.putExtra("report", report);
        startActivity(nextScreen);
    }

    public void onClickNext(View view){
        Intent nextScreen = new Intent(getApplicationContext(), PrintPreviewActivity_.class);
        if(anomalies_list != null) {
            report.anomalies = anomalies_list.getAdapter().getList();
        }
        nextScreen.putExtra("report", report);
        startActivity(nextScreen);
        this.finish();
    }
}
